// RealPlay Ltd  copyrights.
// This program is Open Source AsIs
// You may use it for all purposes, including commercial
// But!!!
// If you use any part of this code, you must state clearly
// both in your source file and in the Help|about of your program,
// that the origin program or code was built by RealPlay Ltd

// ***********************************************************************/
// *                                                                     */
// *        This file is the main unit of "Paint" Project                */
// *                                                                     */
// ***********************************************************************/

// For Paint Project  full ducumentation please refer to the "paint programer.doc"
(*
  The source files in the basic paint program:
  1.  Paint.dpr - the "program file"
  2.  UPMain.pas - (This file) main paint window Defenition & and init section
  3.  UPMenu.pas - Holds the mainmenu and the main action list (the FMenu is a demy form)
  4.  UPActions.pas - Holds code for the basic action like: open file, save file etc'
  5.  UPFrWorkArea.pas Frame of graghic work area - most of the basic graphic procedures
  6.  UPUtil.pas - Common utilitiys used in the project
  7.  UPFloodfill.pas - flood algorithem when color depth <> 0 (inc. the dialog for geting depth from user)
  8.  UPFrameColor in 'UPFrameColor.pas' {ColorFrame: TFrame},
  9.  UPZAct.pas - the algorithem for the graghic Undo & Redo operation (ctrl Z)
  10. UPTools.pas - Frame for graghic tools (brush pipet pen etc' on the left panel
  11. UPFrameColor.pas - Frame for selecting color (on the bottom panal)
  12. UPScaleDlg.pas - Dialog for zoom (magnifer glass)
  13. UPUpdate.pas - A thread of ftp downloading updates
  14. reabout.pas - about form
  15. HelpOverView.pas - help form

  Source for the basic animation extention

  1.  UPFrReal.pas - frame FrRTool holds the animation tools on the right side of the bottom panel
  2.  UP_XML.pas - XML procedure for manipulating the animation
  3.  UPRealLayers.pas - the layers and graphic object of the animation
  4.  UPCarrierMount in 'UPCarrierMount.pas' {FCarrierMount},

*)

// Name convention through the paint project

// UP"something" is for Unit Paint something
// example UPMain is the Main Unit of Paint
// F"something" is for Form (or Dialog Frame) Something
// example FPaint is the Paint main Form (window)
// Fr"something" is for Frame Something
// example FrWorkArea is the Frame holding the Work Area components
{$MINSTACKSIZE $004000}
{$MAXSTACKSIZE $500000}   // critical for the UPfloodfill
unit UPMain;

interface

uses
  UPUtil, UPRealLayers, UPFrWorkArea, UPFrReal, UPFrColor, UPTools,
  UPFloodFill, UPActions, UPMenu, UPUpdate, Dialogs, ExtDlgs, Forms,
  Controls, Buttons, ExtCtrls, Classes, ComCtrls,
  Windows, SysUtils, MPlayer, Graphics, StdCtrls, ImgList;

type

  TFPaint = class(TForm)
    SBar: TStatusBar;
    PanelFullWindow: TPanel;
    PanelLeft: TPanel;
    PanelBottom: TPanel;
    pBottomLeft: TPanel;
    FrTools1: TFrTools;
    FWA: TFrameWorkArea;
    FrRTool1: TFrRTool;
    FrColor1: TFrColor;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    ftpThread: TUpdate;
  public
    { Public declarations }
    AniList: TBMLList;
    IsAni: boolean;
    Tool: integer;
  end;

Var
  ZoomXr: real = 1; // The stretch coefficient for Zoom X Real
  ZoomYr: real = 1; // The stretch coefficient for Zoom Y Real

Const
  // cursor types
  crNone = 0;
  crCatchS = 1;
  crCatch = 2;
  crBin = 4;
  crPipet = 5;
  crPen = 7;
  crBrush = 8;
  crSpray = 9;
  crText = 10;
  crLine = 11;
  crCurve = 12;
  crRect = 13;
  crRectS = 14;
  crCircle = 15;
  crCircleS = 16;
  crTrans = 17;
  crRotate = 20;
  crErasor = 30;
  crErasor1 = 31;
  crErasor2 = 32;
  crErasor3 = 33;
  crMag = 6;
  crUnMag = 61;

  clTrans = $FFFEFF; // key for transperent default color;

var
  FPaint: TFPaint;
  TDbg: integer;

implementation

{$R *.dfm}

// ************************************************************************************************
procedure TFPaint.FormCreate(Sender: TObject);
begin
  FWA.init;
  IsAni := false;
  AniList := TBMLList.create;
  Tool := TDbg;
  Tool := crPen;
  ZoomXr := 1;
  ZoomYr := 1;
  SBar.Panels[5].Text := ' D=' + inttostr(Depth);
{$IF  not Defined(NoUpdate)}
  ftpThread := TUpdate.create(false);
{$IFEND}
end;

// *************************************
procedure TFPaint.FormDestroy(Sender: TObject);
var
  i: integer;
begin
  AniList.Free;
  FWA.finish;
{$IF not Defined(NoUpdate)}
  // this section tryes to shut down ftp thread gracefully
  // but if it will not, it forcefuly shuts it down
  hide;
  i := 0;
  if UpDateOn then
    ftpThread.Terminate;
  while UpDateOn and (i < 20) do
  begin
    sleep(100);
    inc(i);
  end; // let update stop gracefully
  if UpDateOn then
    try
      ftpThread.ftp.Disconnect;
    finally
      TerminateThread(ftpThread.Handle, 0);
    end;
  if not UpDateOn and runupdate then
    winexec('paintupdate.exe', SW_HIDE);
{$IFEND}
end;

// *************************************
procedure TFPaint.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  If SaveOrExit then
    Action := caNone;
end;

// **************************************************************************
procedure TFPaint.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Tool = crMag) and not(ssCtrl in Shift) then
  begin
    screen.cursor := crMag;
    FWA.Im.cursor := crMag;
    screen.cursor := crDefault;
  end;
end;

// **************************
procedure TFPaint.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Tool = crMag) and (ssCtrl in Shift) then
  begin
    screen.cursor := crUnMag;
    FWA.Im.cursor := crUnMag;
    screen.cursor := crDefault;
  end;
  if Key = VK_ESCAPE then
  begin
    FWA.ImSelS.ClearSel;
  end;
end;

// *********************************
procedure TFPaint.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if (Tool = crText) then
    with FWA do
    begin
      FWA.Im.Canvas.Font := Font;
      FWA.Im.Canvas.TextOut(startx,
        starty + 4 - Im.Canvas.Textheight(Key) * 8 div 10, Key);
      startx := startx + Im.Canvas.TextWidth(Key);
      FWA.Mystat.Changed := true;
    end;
end;

initialization

{$R Cursor.res}
// needed new curser shapes imbeded in curser.res  -drawn with delphi image editor

screen.Cursors[crCatchS] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crCatch] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crBin] := LoadCursor(hInstance, 'BIN');
screen.Cursors[crPipet] := LoadCursor(hInstance, 'PIPET');
screen.Cursors[crMag] := LoadCursor(hInstance, 'MAGNIFY');
screen.Cursors[crPen] := LoadCursor(hInstance, 'PEN');
screen.Cursors[crBrush] := LoadCursor(hInstance, 'BRUSH');
screen.Cursors[crSpray] := LoadCursor(hInstance, 'SPRAY');
screen.Cursors[crText] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crLine] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crCurve] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crRect] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crRectS] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crCircle] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crCircleS] := LoadCursor(hInstance, 'CROSS');
screen.Cursors[crTrans] := LoadCursor(hInstance, 'TRANS');
screen.Cursors[crRotate] := LoadCursor(hInstance, 'ROTATE');
screen.Cursors[crErasor] := LoadCursor(hInstance, 'ERASER');
screen.Cursors[crUnMag] := LoadCursor(hInstance, 'UNMAGNIFY')

end.
